// component/order-goods-item/order-goods-item.js
const app = getApp();
const api = require("../../server/Api.js")
Component({
  options: {
    addGlobalClass: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    goods: Object,
    fromPage: String,
    type: Number
  },

  /**
   * 组件的初始数据
   */
  data: {
    toView: "content",
    isMoving: false
  },

  onLoad: function(options) {
    
  },

  /**
   * 组件的方法列表
   */
  methods: {
    lower_count: function() {
      this.doCart(parseInt(this.data.goods.goodsQuantity) - 1, -1);
    },
    upper_count: function() {
      this.doCart(this.data.goods.goodsQuantity ? (parseInt(this.data.goods.goodsQuantity) + 1) : 1, 1);
    },
    doCart: function(goodsCount, num) {
      let userBean = app.globalData.userBean;
      if (!userBean || !userBean.consumerId) {
        tt.navigateTo({
          url: '../login/login',
        })
        return;
      }

      let that = this;
      //服务器加入购物车
      console.log(this.data.goods);
      let skuId = this.data.goods.skuid || this.data.goods.skuId;
      let userId = app.globalData.userBean.consumerId;
      let recId = this.data.goods.recId;
      if (goodsCount === 0 && recId) {
        that.triggerEvent('delCart', {
          goods: this.data.goods
        })
      } else if (goodsCount === 1 && num === 1) {
        api.addCart(goodsCount, skuId, userId).then(res => {
          that.data.goods.recId = res.recId;
          that.data.goods.goodsQuantity = goodsCount;
          that.setData({
            goods: that.data.goods
          });
          this.setPageUpdate(skuId, num);
        }).catch(msg => {
          let message = typeof msg == "string" ? msg : msg.message;
          app.showToast(message)
        })
      } else {
        api.updateCartCount(recId, goodsCount).then(res => {
          that.data.goods.goodsQuantity = goodsCount;
          that.setData({
            goods: that.data.goods
          });
          this.setPageUpdate(skuId, num);
        }).catch(msg => {
          let message = typeof msg == "string" ? msg : msg.message;
          app.showToast(message);
        })
      }
    },
    setPageUpdate(skuid, num, goodsQuantity) {
      if (this.data.fromPage === "index") {
        app.globalData.cartUpdated = false;
      } else if (this.data.fromPage === "cart") {
        app.globalData.indexCartUpdated = false;
      }
      this.triggerEvent('refreshCart', {
        num: num,
        skuId: skuid,
        goods: this.data.goods
      })
      app.globalData.cartCount += num;
      app.setTabBarBadge();
    },
    goDetail: function(e) {
      if (this.data.goods.id)
        tt.navigateTo({
          url: '../goods-detail/goods-detail?id=' + this.data.goods.id,
        })
    },
    scroll(e) {
      console.log(e.detail.deltaX, e.detail.scrollLeft, (136 * app.globalData.rpxToPxBl) / 2);
      let that = this;
      if (!that.data.isMoving) {
        if (e.detail.scrollLeft > 0) {
          if (e.detail.deltaX < 2) {
            // that.triggerEvent('leftMove', {
            //   cur: that.data.goods
            // });
            that.setData({
              isMoving: true,
              toView: "del"
            })
            setTimeout(() => {
              that.setData({
                isMoving: false
              })
            }, 300)
          } else if (e.detail.deltaX > 2) {
            that.setData({
              isMoving: true,
              toView: "content"
            })
            setTimeout(() => {
              that.setData({
                isMoving: false
              })
            }, 300)
          }
        }
      }
    },
    //删除事件
    del: function(e) {
      this.triggerEvent('delCart', {
        goods: this.data.goods
      })
    }
  }
})